package com.rjwh.dingdong.client.activity.homework;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.homework.fragment.HomeWorkFansListFragment;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.view.HackyViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 王小珂
 * @ClassName HomeWorkFansActivity
 * @Description
 * @date 2017-1-6
 * @history 2017-1-6 author: description:
 */

public class HomeWorkFansActivity extends BaseActivity {

    @BindView(R.id.draw_book_catalog_title_btn1)
    Button drawBookCatalogTitleBtn1;
    @BindView(R.id.draw_book_catalog_title_btn2)
    Button drawBookCatalogTitleBtn2;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.home_work_fans_viewpager)
    HackyViewPager homeWorkFansViewpager;
    @BindView(R.id.draw_book_catalog_tab_root_ll)
    LinearLayout drawBookCatalogTabRootLl;
    private List<BaseFragment> listFragment = new ArrayList<>();

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, HomeWorkFansActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_work_fans_layout);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        initToolbarHelper();
        listFragment.add(HomeWorkFansListFragment.newInstance(1));//我的粉丝
        listFragment.add(HomeWorkFansListFragment.newInstance(2));//我的关注
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
        homeWorkFansViewpager.setAdapter(adapter);
        homeWorkFansViewpager.addOnPageChangeListener(new HackyViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updateTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * init the toolbar
     */
    protected void initToolbarHelper() {
        toolbar.setTitle("");
        this.setSupportActionBar(this.toolbar);
        getSupportActionBar().setTitle("");
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        drawBookCatalogTabRootLl.setVisibility(View.VISIBLE);
        drawBookCatalogTitleBtn1.setText(getString(R.string.homework_fans));
        drawBookCatalogTitleBtn2.setText(getString(R.string.homework_follows));
    }

    /* ***************FragmentPagerAdapter***************** */
    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return listFragment.size();
        }

        @Override
        public Fragment getItem(int position) {
            return listFragment.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return super.isViewFromObject(view, object);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // super.destroyItem(container, position, object);
        }
    }

    @OnClick({R.id.draw_book_catalog_title_btn1, R.id.draw_book_catalog_title_btn2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.draw_book_catalog_title_btn1:
                updateTab(0);
                break;
            case R.id.draw_book_catalog_title_btn2:
                updateTab(1);
                break;
        }
    }

    private void updateTab(int position) {
        switch (position) {
            case 0:
                drawBookCatalogTitleBtn1.setClickable(false);
                drawBookCatalogTitleBtn2.setClickable(true);
                drawBookCatalogTitleBtn1.setTextColor(getResources().getColor(R.color.white));
                drawBookCatalogTitleBtn2.setTextColor(getResources().getColor(R.color.blue_button_nor));
                drawBookCatalogTitleBtn1.setBackgroundResource(R.drawable.blue_tab_left_pressed);
                drawBookCatalogTitleBtn2.setBackgroundResource(R.drawable.blue_tab_right_normal);
                //选中的为"国学绘本"
                homeWorkFansViewpager.setCurrentItem(0);
                HomeWorkFansListFragment catalogFragment1 = (HomeWorkFansListFragment) listFragment.get(0);
                catalogFragment1.doRefresh();
                break;
            case 1:
                drawBookCatalogTitleBtn1.setClickable(true);
                drawBookCatalogTitleBtn2.setClickable(false);
                drawBookCatalogTitleBtn1.setTextColor(getResources().getColor(R.color.blue_button_nor));
                drawBookCatalogTitleBtn2.setTextColor(getResources().getColor(R.color.white));
                drawBookCatalogTitleBtn1.setBackgroundResource(R.drawable.blue_tab_left_normal);
                drawBookCatalogTitleBtn2.setBackgroundResource(R.drawable.blue_tab_right_pressed);
                //选中的为"分级阅读"
                homeWorkFansViewpager.setCurrentItem(1);
                HomeWorkFansListFragment catalogFragment2 = (HomeWorkFansListFragment) listFragment.get(1);
                catalogFragment2.doRefresh();
                break;

            default:
                break;
        }

    }
}
